package com.servlet.dao;

import com.servlet.bean.User;
import com.servlet.utils.JDBCHelper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Title: UserDao
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2023/12/25 15:05
 */
public class UserDao {

    /**
     * 增加用户
     * @param user
     * @throws SQLException
     */
    public void addUser(User user) throws SQLException {
        Connection connection = JDBCHelper.getConnection();
        String sql = "insert into t_user(username,password) values(?,?);";
        QueryRunner queryRunner = new QueryRunner();
        queryRunner.update(connection,sql,user.getUsername(),user.getPassword());
    }

    public User queryUserByUsername(String username) throws SQLException {
        Connection connection = JDBCHelper.getConnection();
        String sql = "select * from t_user where username = ?;";
        QueryRunner queryRunner = new QueryRunner();
         User user = queryRunner.query(connection,sql,new BeanHandler<User>(User.class),username);
         return user;
    }
}
